package com.example.safeassistan.home.activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;

import android.view.View;
import android.widget.Toast;
import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.safeassistan.R;
import com.example.safeassistan.base.BaseActivity;
import com.example.safeassistan.constants.UrlConstants;
import com.example.safeassistan.hiddendanger.model.DepartMentAllModel;
import com.example.safeassistan.hiddendanger.model.HiddenDangerTypeModel;
import com.example.safeassistan.home.adapter.AuthorizedepartmentAdapter;
import com.example.safeassistan.home.adapter.HiddenDangerTypeAdapter;
import com.example.safeassistan.http.HttpRequest;
import com.example.safeassistan.http.HttpStringCallBack;
import com.example.safeassistan.utile.JsonUtils;
import org.json.JSONObject;


public class ActivityHiddenDangerType extends BaseActivity implements View.OnClickListener {
    private HiddenDangerTypeAdapter adapter;
    private RecyclerView rcView;

    @Override
    protected void init(Bundle savedInstanceState) {
        setAndroidNativeLightStatusBar(true);
    }

    @Override
    protected int setLayoutResourceID() {
        return R.layout.activity_hiddendanger_type;
    }

    @Override
    protected void initView() {
        setTitleText("类型选择");
        rcView=findViewById(R.id.rcCommon);
        getData();
    }

    @Override
    protected void initData() { }

    @Override
    protected int getTitleBarType() {
        return 0;
    }

    public void getData(){
        new HttpRequest(this).doGet(UrlConstants.queryDepartTypeList, null, new HttpStringCallBack() {
            @RequiresApi(api = Build.VERSION_CODES.M)
            @Override
            public void onSuccess(Object result) {
                String datas = result.toString();
                JSONObject jsonObject = null;
                try {
                    jsonObject = new JSONObject(datas);
                    String message = jsonObject.getString("message");
                    Boolean success = jsonObject.getBoolean("success");
                    if (success) {
                        Object obj = JsonUtils.fromJson(result.toString(), HiddenDangerTypeModel.class);
                        if (obj instanceof HiddenDangerTypeModel) {
                            HiddenDangerTypeModel model=(HiddenDangerTypeModel) obj;
                            adapter=new HiddenDangerTypeAdapter(model.getResult());
                            rcView.setLayoutManager(new LinearLayoutManager(getContext()));
                            rcView.setAdapter(adapter);
                            adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                                @Override
                                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                                    Intent intent = new Intent();
                                    intent.putExtra("id",model.getResult().get(position).getId());
                                    intent.putExtra("name",model.getResult().get(position).getNum());
                                    setResult(RESULT_OK, intent);
                                    finish();
                                }
                            });
                        }
                    }else {
                        Toast.makeText(ActivityHiddenDangerType.this,message,Toast.LENGTH_SHORT).show();
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(int code, String msg) {

            }
        });
    }

    /**
     * Called when a view has been clicked.
     *
     * @param v The view that was clicked.
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tvSearch:
                getData();
                break;
        }
    }
}
